package com.java.objectstreamdemo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.jupiter.api.Test;

public class TestDriver {
	/**
	 * 对象输出流的使用
	 */
	@Test
	public void test01() {
		// 创建一个学生的实例对象，即内存中的快照
		Student stu0 = new Student("张三", 20, "男");
		// 创建一个外部文件类型
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:/temp/objectstream/obj.dat"));) {
			// 将对象写出
			oos.writeObject(stu0);
			System.out.println(stu0);
			System.out.println("ok!");

		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	/**
	 * 对象输入流的使用
	 */
	@Test
	public void test02() {
		// 将信息输入到内存中
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:/temp/objectstream/obj.txt"));) {
			Object stu0 = ois.readObject();
			System.out.println("读入的内容：" + stu0);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Test
	public void test03() {
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:/temp/objectstream/obj.txt"));
				ObjectOutputStream oos = new ObjectOutputStream(
						new FileOutputStream("E:/temp/objectstream/obj.dat"));) {
			// 创建一个学生对象
			Student stu0 = new Student("张三", 20, "男");
			// 写出内容
			oos.writeObject(stu0);
			System.out.println("写出的内容：" + stu0);
			// 写出
			Object stu01 = ois.readObject();
			System.out.println("读出的内容：" + stu01);
			// 判断stu0是否等于stu01
			System.out.println("stu0==stu01::" + stu0 == stu01);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void test04() {

	}
}
